package com.live.dengdengtv.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.live.dengdengtv.BaseActivity;
import com.live.dengdengtv.R;
import com.live.dengdengtv.activity.live.ActivityLiveTitle;
import com.live.dengdengtv.activity.msg.FriendListActivity;
import com.live.dengdengtv.activity.msg.MyMsgActivity;
import com.live.dengdengtv.fragment.hall.HallFragment;
import com.live.dengdengtv.fragment.hall.MyInfoFragent;
import com.live.dengdengtv.kernel.avcontrollers.QavsdkControl;
import com.live.dengdengtv.utils.Notice;

public class MainHallActivity extends BaseActivity implements View.OnClickListener{

    private static final String TAG = "MainHallActivity";
    private HallFragment hallFragment;
    private MyInfoFragent myInfoFragent;
    private Button hall_bt;
    private ImageView img_room;
    private Button myinfo_bt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_hall);

        hallFragment = new HallFragment();
        myInfoFragent = new MyInfoFragent();

        if (savedInstanceState == null) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.content_fragment, hallFragment);
            transaction.add(R.id.content_fragment, myInfoFragent);
            transaction.hide(myInfoFragent);
            transaction.commit();
        }

        findViewById(R.id.title_left_iv).setOnClickListener(this);
        findViewById(R.id.msg_iv).setOnClickListener(this);
        findViewById(R.id.follow_iv).setOnClickListener(this);

        hall_bt = (Button) findViewById(R.id.hall_bt);
        img_room = (ImageView) findViewById(R.id.img_room);
        myinfo_bt = (Button) findViewById(R.id.myinfo_bt);

        setHallChecked(true);
        setRoomChecked(false);
        setMyInfoChecked(false);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_left_iv :
                goSearch(); break;
            case R.id.follow_iv :
                goFollowActivity(); break;
            case R.id.msg_iv :
                goMsgActivity(); break;
            case R.id.img_room :
                goRoom(); break;
            case R.id.hall_bt :
                goHall(); break;
            case R.id.myinfo_bt :
                goMyInfo(); break;
        }
    }

    private void goFollowActivity() {
        Intent intent = new Intent(this, FriendListActivity.class);
        startActivity(intent);
    }

    private void goMsgActivity() {
        Intent intent = new Intent(this, MyMsgActivity.class);
        startActivity(intent);
    }

    private void goSearch() {
        Intent intent = new Intent(this, SearchActivity.class);
        startActivity(intent);
    }

    private void goHall() {
        setHallChecked(true);
        setRoomChecked(false);
        setMyInfoChecked(false);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(myInfoFragent);
        transaction.show(hallFragment);
        transaction.commit();
    }

    private void goMyInfo() {
        setHallChecked(false);
        setRoomChecked(false);
        setMyInfoChecked(true);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(hallFragment);
        transaction.show(myInfoFragent);
        transaction.commit();
    }

    private void goRoom(){
        setHallChecked(false);
        setRoomChecked(true);
        setMyInfoChecked(false);

        Intent intent = new Intent(this, ActivityLiveTitle.class);
//        intent.putExtra(Constants.ID_STATUS, Constants.HOST);
//        MySelfInfo.getInstance().setIdStatus(Constants.HOST);
//        MySelfInfo.getInstance().setJoinRoomWay(true);
//        CurLiveInfo.setTitle("齐天大神直播!");
//        CurLiveInfo.setHostID(MySelfInfo.getInstance().getId());
//        CurLiveInfo.setRoomNum(MySelfInfo.getInstance().getMyRoomNum());
        startActivity(intent);
//        SxbLog.i(TAG, "PerformanceTest  publish Live     " + SxbLog.getTime());
    }

    private void setHallChecked(boolean checked){
        int textColor = 0xFFFFFFFF;
        int resId = R.mipmap.hall_bottom_left_normal;
        if(checked){
            textColor = 0xFFED242C;
            resId = R.mipmap.hall_bottom_left_press;
        }

        hall_bt.setTextColor(textColor);
        Drawable drawable = context.getResources().getDrawable(resId);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//必须设置图片大小，否则不显示
        hall_bt.setCompoundDrawables(null, drawable, null, null);
    }
    private void setMyInfoChecked(boolean checked){
        int textColor = 0xFFFFFFFF;
        int resId = R.mipmap.hall_bottom_right_normal;
        if(checked){
            textColor = 0xFFED242C;
            resId = R.mipmap.hall_bottom_right_press;
        }

        myinfo_bt.setTextColor(textColor);
        Drawable drawable = context.getResources().getDrawable(resId);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//必须设置图片大小，否则不显示
        myinfo_bt.setCompoundDrawables(null, drawable, null, null);
    }
    private void setRoomChecked(boolean checked){
        if(checked){
            img_room.setImageResource(R.mipmap.hall_bottom_center_press);
        }else {
            img_room.setImageResource(R.mipmap.hall_bottom_center_normal);
        }
    }

    @Override
    public void onBackPressed() {

        Notice.confirm(context, "确定退出?", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
            }
        }, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        QavsdkControl.getInstance().stopContext();
    }
}
